{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-02T13:01:18.203540Z",
     "start_time": "2025-04-02T13:01:18.188896Z"
    }
   },
   "source": [
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "import os"
   ],
   "outputs": [],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:01:23.675824Z",
     "start_time": "2025-04-02T13:01:19.808198Z"
    }
   },
   "cell_type": "code",
   "source": [
    "first_order_df = pd.read_csv('data/first_order_df.csv')\n",
    "first_order_df.drop(['Unnamed: 0'], axis=1, inplace=True)\n",
    "first_order_df"
   ],
   "id": "d49e244b1bdac33",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                                                   TxHash  BlockHeight  \\\n",
       "0       0xaca3850ba0080cf47b47f80e46da452f61bcbb5470d3...      5848095   \n",
       "1       0x95681862f9778e49caecf603dd911d6ed57f7799d89d...      5848181   \n",
       "2       0x716ae3961b50186a0bbc272cfcc4555662f7fe33550f...      5848716   \n",
       "3       0xf397197b800d6cc055a4db265b5e9df3dd2aa745c813...      5849038   \n",
       "4       0x7f8086011a32f128dba57fe06fc5f4a181d2f5401e5a...      5849437   \n",
       "...                                                   ...          ...   \n",
       "254968  0xefbdb95e7e0dae4f83f975a44c7ccf7e6b027131969e...      6101678   \n",
       "254969  0x37c0af73723405236804486c835282a1971e08a45a11...      6104069   \n",
       "254970  0xdd193f8d9ebf7eecd3ad59192a807b420e3a5d38ff1a...      6104111   \n",
       "254971  0x1b3329bdb6c8d6a817ebe55bfd6434ece5cb23817b0d...      6104810   \n",
       "254972  0x14d396011198cf19cf6cc58f4ea479b9cb9cbc264904...      6460152   \n",
       "\n",
       "         TimeStamp                                        From  \\\n",
       "0       1529873859  0x16f209b5332a1b4fa5bf19497ca40154c5db2f85   \n",
       "1       1529875104  0xe7e07e44ee315b5f2d076340b2b7a5cc9a4ee57b   \n",
       "2       1529883192  0x002f0c8119c16d310342d869ca8bf6ace34d9c39   \n",
       "3       1529887684  0x0681d8db095565fe8a346fa0277bffde9c0edbbf   \n",
       "4       1529893144  0x002f0c8119c16d310342d869ca8bf6ace34d9c39   \n",
       "...            ...                                         ...   \n",
       "254968  1533603040  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "254969  1533638323  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "254970  1533639008  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "254971  1533649370  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "254972  1538773934  0x7238802e995dc4834c8fb430275492179e999c98   \n",
       "\n",
       "                                                To     Value  isError  \n",
       "0       0x002f0c8119c16d310342d869ca8bf6ace34d9c39  0.500000        0  \n",
       "1       0x002f0c8119c16d310342d869ca8bf6ace34d9c39  0.001020        0  \n",
       "2       0xe892875b87b94c44edf0e91ee9f49d0525fadd83  0.500390        0  \n",
       "3       0x002f0c8119c16d310342d869ca8bf6ace34d9c39  0.817800        0  \n",
       "4       0xe892875b87b94c44edf0e91ee9f49d0525fadd83  0.817506        0  \n",
       "...                                            ...       ...      ...  \n",
       "254968  0x725d0607775d6e8e85136e51808b9492e39d217d  0.700000        0  \n",
       "254969  0x5d63c7b81aaa63d2df376c0bc6197ab5132e94e7  0.770000        0  \n",
       "254970  0x1219320139334c3d79aae0507a2aa9e278935665  0.500000        0  \n",
       "254971  0xf3702667fb827bd9c675b88187e9a764b5dc9d40  0.330000        0  \n",
       "254972  0xffde23396d57e10abf58bd929bb1e856c7718218  0.500000        0  \n",
       "\n",
       "[254973 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>TxHash</th>\n",
       "      <th>BlockHeight</th>\n",
       "      <th>TimeStamp</th>\n",
       "      <th>From</th>\n",
       "      <th>To</th>\n",
       "      <th>Value</th>\n",
       "      <th>isError</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0xaca3850ba0080cf47b47f80e46da452f61bcbb5470d3...</td>\n",
       "      <td>5848095</td>\n",
       "      <td>1529873859</td>\n",
       "      <td>0x16f209b5332a1b4fa5bf19497ca40154c5db2f85</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0x95681862f9778e49caecf603dd911d6ed57f7799d89d...</td>\n",
       "      <td>5848181</td>\n",
       "      <td>1529875104</td>\n",
       "      <td>0xe7e07e44ee315b5f2d076340b2b7a5cc9a4ee57b</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0.001020</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0x716ae3961b50186a0bbc272cfcc4555662f7fe33550f...</td>\n",
       "      <td>5848716</td>\n",
       "      <td>1529883192</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0xe892875b87b94c44edf0e91ee9f49d0525fadd83</td>\n",
       "      <td>0.500390</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0xf397197b800d6cc055a4db265b5e9df3dd2aa745c813...</td>\n",
       "      <td>5849038</td>\n",
       "      <td>1529887684</td>\n",
       "      <td>0x0681d8db095565fe8a346fa0277bffde9c0edbbf</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0.817800</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0x7f8086011a32f128dba57fe06fc5f4a181d2f5401e5a...</td>\n",
       "      <td>5849437</td>\n",
       "      <td>1529893144</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0xe892875b87b94c44edf0e91ee9f49d0525fadd83</td>\n",
       "      <td>0.817506</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254968</th>\n",
       "      <td>0xefbdb95e7e0dae4f83f975a44c7ccf7e6b027131969e...</td>\n",
       "      <td>6101678</td>\n",
       "      <td>1533603040</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0x725d0607775d6e8e85136e51808b9492e39d217d</td>\n",
       "      <td>0.700000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254969</th>\n",
       "      <td>0x37c0af73723405236804486c835282a1971e08a45a11...</td>\n",
       "      <td>6104069</td>\n",
       "      <td>1533638323</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0x5d63c7b81aaa63d2df376c0bc6197ab5132e94e7</td>\n",
       "      <td>0.770000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254970</th>\n",
       "      <td>0xdd193f8d9ebf7eecd3ad59192a807b420e3a5d38ff1a...</td>\n",
       "      <td>6104111</td>\n",
       "      <td>1533639008</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0x1219320139334c3d79aae0507a2aa9e278935665</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254971</th>\n",
       "      <td>0x1b3329bdb6c8d6a817ebe55bfd6434ece5cb23817b0d...</td>\n",
       "      <td>6104810</td>\n",
       "      <td>1533649370</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0xf3702667fb827bd9c675b88187e9a764b5dc9d40</td>\n",
       "      <td>0.330000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254972</th>\n",
       "      <td>0x14d396011198cf19cf6cc58f4ea479b9cb9cbc264904...</td>\n",
       "      <td>6460152</td>\n",
       "      <td>1538773934</td>\n",
       "      <td>0x7238802e995dc4834c8fb430275492179e999c98</td>\n",
       "      <td>0xffde23396d57e10abf58bd929bb1e856c7718218</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>254973 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:01:29.361015Z",
     "start_time": "2025-04-02T13:01:28.557433Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from_counts = first_order_df['From'].value_counts()\n",
    "to_counts = first_order_df['To'].value_counts()\n",
    "combined_counts = from_counts.add(to_counts, fill_value=0)\n",
    "\n",
    "# to remove all values whose transcation count is less than 5 or greater than 500\n",
    "filtered_counts = combined_counts.loc[(combined_counts >= 5) & (combined_counts <=500)]\n",
    "\n",
    "#dropping all unnecessary columns\n",
    "values_to_keep = filtered_counts.index.tolist()\n",
    "filtered_df = first_order_df[first_order_df['From'].isin(values_to_keep) & first_order_df['To'].isin(values_to_keep)]\n",
    "filtered_df = filtered_df.reset_index(drop=True)\n",
    "filtered_df"
   ],
   "id": "ccd35d7ebc7e2d14",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                                                  TxHash  BlockHeight  \\\n",
       "0      0x716ae3961b50186a0bbc272cfcc4555662f7fe33550f...      5848716   \n",
       "1      0x7f8086011a32f128dba57fe06fc5f4a181d2f5401e5a...      5849437   \n",
       "2      0xb8a7330af45b7be8155e4ffe437110ce912cb7986632...      4981935   \n",
       "3      0xd3e26b0a15a76c1fda5ee297924c7a46b75811588fb3...      4982190   \n",
       "4      0xf1df92e16295368daf93e3cf793359f6ba8ef2d3e2e5...      4982642   \n",
       "...                                                  ...          ...   \n",
       "43548  0x020f75601c9fc8a5029a65c492e5753cda3a3a20efdc...      6064839   \n",
       "43549  0x6da824b9a5b3e5f1e416a4a5f58cec67375e89d21e88...      6064851   \n",
       "43550  0x40dfa46e27534c52c174e3f2c503677d546ce03b6406...      6064861   \n",
       "43551  0xbd0352624b75713beaa77116e5ac1271b44f87d70d46...      6064872   \n",
       "43552  0x1b3329bdb6c8d6a817ebe55bfd6434ece5cb23817b0d...      6104810   \n",
       "\n",
       "        TimeStamp                                        From  \\\n",
       "0      1529883192  0x002f0c8119c16d310342d869ca8bf6ace34d9c39   \n",
       "1      1529893144  0x002f0c8119c16d310342d869ca8bf6ace34d9c39   \n",
       "2      1517057137  0xcbb240ad8beb648773d78d966caaee46d45add3a   \n",
       "3      1517060992  0xc90873d0c4b07db2327676d3b3f2add807d5fc0e   \n",
       "4      1517067252  0xfffa5f2b2cb0b916ef69cc23cacf02f34abd0a79   \n",
       "...           ...                                         ...   \n",
       "43548  1533065124  0x2a4127e646f0ec5d39d604d9085a88296049d337   \n",
       "43549  1533065246  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "43550  1533065387  0x2a4127e646f0ec5d39d604d9085a88296049d337   \n",
       "43551  1533065544  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "43552  1533649370  0xff8e6af02d41a576a0c82f7835535193e1a6bccc   \n",
       "\n",
       "                                               To     Value  isError  \n",
       "0      0xe892875b87b94c44edf0e91ee9f49d0525fadd83  0.500390        0  \n",
       "1      0xe892875b87b94c44edf0e91ee9f49d0525fadd83  0.817506        0  \n",
       "2      0x0059b14e35dab1b4eee1e2926c7a5660da66f747  0.002305        0  \n",
       "3      0x0059b14e35dab1b4eee1e2926c7a5660da66f747  0.014261        0  \n",
       "4      0x0059b14e35dab1b4eee1e2926c7a5660da66f747  0.008121        0  \n",
       "...                                           ...       ...      ...  \n",
       "43548  0xff8e6af02d41a576a0c82f7835535193e1a6bccc  0.068800        0  \n",
       "43549  0x2a4127e646f0ec5d39d604d9085a88296049d337  0.229000        0  \n",
       "43550  0xff8e6af02d41a576a0c82f7835535193e1a6bccc  0.228910        0  \n",
       "43551  0x2a4127e646f0ec5d39d604d9085a88296049d337  0.025000        0  \n",
       "43552  0xf3702667fb827bd9c675b88187e9a764b5dc9d40  0.330000        0  \n",
       "\n",
       "[43553 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>TxHash</th>\n",
       "      <th>BlockHeight</th>\n",
       "      <th>TimeStamp</th>\n",
       "      <th>From</th>\n",
       "      <th>To</th>\n",
       "      <th>Value</th>\n",
       "      <th>isError</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0x716ae3961b50186a0bbc272cfcc4555662f7fe33550f...</td>\n",
       "      <td>5848716</td>\n",
       "      <td>1529883192</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0xe892875b87b94c44edf0e91ee9f49d0525fadd83</td>\n",
       "      <td>0.500390</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0x7f8086011a32f128dba57fe06fc5f4a181d2f5401e5a...</td>\n",
       "      <td>5849437</td>\n",
       "      <td>1529893144</td>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0xe892875b87b94c44edf0e91ee9f49d0525fadd83</td>\n",
       "      <td>0.817506</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0xb8a7330af45b7be8155e4ffe437110ce912cb7986632...</td>\n",
       "      <td>4981935</td>\n",
       "      <td>1517057137</td>\n",
       "      <td>0xcbb240ad8beb648773d78d966caaee46d45add3a</td>\n",
       "      <td>0x0059b14e35dab1b4eee1e2926c7a5660da66f747</td>\n",
       "      <td>0.002305</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0xd3e26b0a15a76c1fda5ee297924c7a46b75811588fb3...</td>\n",
       "      <td>4982190</td>\n",
       "      <td>1517060992</td>\n",
       "      <td>0xc90873d0c4b07db2327676d3b3f2add807d5fc0e</td>\n",
       "      <td>0x0059b14e35dab1b4eee1e2926c7a5660da66f747</td>\n",
       "      <td>0.014261</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0xf1df92e16295368daf93e3cf793359f6ba8ef2d3e2e5...</td>\n",
       "      <td>4982642</td>\n",
       "      <td>1517067252</td>\n",
       "      <td>0xfffa5f2b2cb0b916ef69cc23cacf02f34abd0a79</td>\n",
       "      <td>0x0059b14e35dab1b4eee1e2926c7a5660da66f747</td>\n",
       "      <td>0.008121</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43548</th>\n",
       "      <td>0x020f75601c9fc8a5029a65c492e5753cda3a3a20efdc...</td>\n",
       "      <td>6064839</td>\n",
       "      <td>1533065124</td>\n",
       "      <td>0x2a4127e646f0ec5d39d604d9085a88296049d337</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0.068800</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43549</th>\n",
       "      <td>0x6da824b9a5b3e5f1e416a4a5f58cec67375e89d21e88...</td>\n",
       "      <td>6064851</td>\n",
       "      <td>1533065246</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0x2a4127e646f0ec5d39d604d9085a88296049d337</td>\n",
       "      <td>0.229000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43550</th>\n",
       "      <td>0x40dfa46e27534c52c174e3f2c503677d546ce03b6406...</td>\n",
       "      <td>6064861</td>\n",
       "      <td>1533065387</td>\n",
       "      <td>0x2a4127e646f0ec5d39d604d9085a88296049d337</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0.228910</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43551</th>\n",
       "      <td>0xbd0352624b75713beaa77116e5ac1271b44f87d70d46...</td>\n",
       "      <td>6064872</td>\n",
       "      <td>1533065544</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0x2a4127e646f0ec5d39d604d9085a88296049d337</td>\n",
       "      <td>0.025000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43552</th>\n",
       "      <td>0x1b3329bdb6c8d6a817ebe55bfd6434ece5cb23817b0d...</td>\n",
       "      <td>6104810</td>\n",
       "      <td>1533649370</td>\n",
       "      <td>0xff8e6af02d41a576a0c82f7835535193e1a6bccc</td>\n",
       "      <td>0xf3702667fb827bd9c675b88187e9a764b5dc9d40</td>\n",
       "      <td>0.330000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43553 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:04:42.324883Z",
     "start_time": "2025-04-02T13:04:42.299798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "first_order_df['isError'].value_counts()"
   ],
   "id": "5fcb53dbc69b548f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "isError\n",
       "0    239339\n",
       "1     15634\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:04:47.721945Z",
     "start_time": "2025-04-02T13:04:44.334146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "import networkx as nx\n",
    "\n",
    "# Create a directed graph\n",
    "graph = nx.DiGraph()\n",
    "\n",
    "# Add edges to the graph\n",
    "for i in range(len(filtered_df)):\n",
    "  graph.add_edge(filtered_df['From'][i],filtered_df['To'][i])"
   ],
   "id": "329077d4133fa261",
   "outputs": [],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:04:49.387898Z",
     "start_time": "2025-04-02T13:04:49.361919Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from tqdm import tqdm\n",
    "walk_length = 10\n",
    "\n",
    "def get_randomwalk(node, path_length):\n",
    "    random_walk = [node]\n",
    "    for i in range(path_length-1):\n",
    "        temp = list(graph.neighbors(node))\n",
    "        temp = list(set(temp) - set(random_walk))\n",
    "        if len(temp) == 0:\n",
    "            break\n",
    "\n",
    "        random_node = random.choice(temp)\n",
    "        random_walk.append(random_node)\n",
    "        node = random_node\n",
    "    return random_walk"
   ],
   "id": "912687b8715e4bc3",
   "outputs": [],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:04:51.178163Z",
     "start_time": "2025-04-02T13:04:51.163164Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_nodes = list(graph.nodes())"
   ],
   "id": "a650a27c80b94eac",
   "outputs": [],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:05:45.049666Z",
     "start_time": "2025-04-02T13:04:54.478623Z"
    }
   },
   "cell_type": "code",
   "source": [
    "walk_per_node = [100,200,300]\n",
    "dictionaries = {}\n",
    "for i in range(len(walk_per_node)):\n",
    "  dict_name = 'D' + str(i+1)\n",
    "  stats = {}\n",
    "  random_walks = []\n",
    "  for n in tqdm(all_nodes):\n",
    "    for j in range(walk_per_node[i]):\n",
    "      random_walks.append(get_randomwalk(n,walk_length))\n",
    "  total_edges=0\n",
    "  for k in random_walks:\n",
    "    total_edges+=len(k)\n",
    "  stats['Labelled Nodes'] = walk_per_node[i]\n",
    "  stats['Edges'] = total_edges\n",
    "  stats['Average Degree'] = total_edges/(walk_per_node[i]*len(all_nodes))\n",
    "  dictionaries[dict_name] = stats"
   ],
   "id": "48506c1e66ac357d",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4874/4874 [00:06<00:00, 765.80it/s] \n",
      "100%|██████████| 4874/4874 [00:15<00:00, 320.05it/s]\n",
      "100%|██████████| 4874/4874 [00:26<00:00, 181.34it/s]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:07:18.345457Z",
     "start_time": "2025-04-02T13:07:18.329589Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dictionaries"
   ],
   "id": "ae8dc0ce12e83eaf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'D1': {'Labelled Nodes': 100,\n",
       "  'Edges': 1055300,\n",
       "  'Average Degree': 2.1651620845301602},\n",
       " 'D2': {'Labelled Nodes': 200,\n",
       "  'Edges': 2112376,\n",
       "  'Average Degree': 2.1669839967172755},\n",
       " 'D3': {'Labelled Nodes': 300,\n",
       "  'Edges': 3167727,\n",
       "  'Average Degree': 2.166411571604432}}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:09:43.556014Z",
     "start_time": "2025-04-02T13:07:23.400741Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from gensim.models import Word2Vec\n",
    "model = Word2Vec(window = 4, sg = 1, hs = 0, negative = 10, alpha=0.03, min_alpha=0.0007, seed = 14)\n",
    "model.build_vocab(random_walks, progress_per=2)\n",
    "model.train(random_walks, total_examples = model.corpus_count, epochs=20, report_delay=1)\n",
    "\n",
    "model.wv"
   ],
   "id": "302aa4ba7e35c2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<gensim.models.keyedvectors.KeyedVectors at 0x2329a73fdc0>"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:10:13.924148Z",
     "start_time": "2025-04-02T13:10:13.907257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "# Calculate the degrees for all nodes\n",
    "degrees = graph.degree()\n",
    "in_degrees = graph.in_degree()\n",
    "out_degrees = graph.out_degree()\n",
    "\n",
    "data = []\n",
    "nodes = graph.nodes()"
   ],
   "id": "3092056025098157",
   "outputs": [],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:14:14.696333Z",
     "start_time": "2025-04-02T13:10:16.300785Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for node in tqdm(list(nodes)):\n",
    "    degree = degrees[node]\n",
    "    in_degree = in_degrees[node]\n",
    "    out_degree = out_degrees[node]\n",
    "    in_degree_ratio = in_degree / out_degree if out_degree != 0 else float('inf')\n",
    "    out_degree_ratio = out_degree / in_degree if in_degree != 0 else float('inf')\n",
    "    transfer_out = filtered_df.loc[filtered_df['From'] == node, 'Value'].sum()\n",
    "    transfer_in = filtered_df.loc[filtered_df['To'] == node, 'Value'].sum()\n",
    "    transaction = transfer_out+transfer_in\n",
    "    transaction_diff = transfer_in-transfer_out\n",
    "    transaction_ratio = transfer_in / transfer_out if transfer_out != 0 else float('inf')\n",
    "    transfer_in_ratio = transfer_in / in_degree if in_degree != 0 else float('inf')\n",
    "    transfer_out_ratio = transfer_out / out_degree if out_degree != 0 else float('inf')\n",
    "    neighbours = len(set(graph.neighbors(node)))\n",
    "\n",
    "    timestamp_diff = ((filtered_df.loc[filtered_df['From'] == node, 'TimeStamp'].diff()).sum() + (filtered_df.loc[filtered_df['To'] == node, 'TimeStamp'].diff()).sum())\n",
    "    avg_timestamp_diff = timestamp_diff/(len(filtered_df.loc[filtered_df['From'] == node, 'TimeStamp'])+len(filtered_df.loc[filtered_df['To'] == node, 'TimeStamp']))\n",
    "    inv_timestamp_freq = 1/avg_timestamp_diff\n",
    "    node_dict = {\"Node\": node,\n",
    "                 \"Total Degree\": degree,\n",
    "                 \"Out-Degree\": out_degree,\n",
    "                 \"In-Degree\": in_degree,\n",
    "                 \"Out-Degree Ratio\": out_degree_ratio,\n",
    "                 \"In-Degree Ratio\": in_degree_ratio,\n",
    "                 \"Sum of Transactions\": transaction,\n",
    "                 \"Transfer-Out Transaction\": transfer_out,\n",
    "                 \"Transfer-In Transaction\": transfer_in,\n",
    "                 \"Transaction Difference\": transaction_diff,\n",
    "                 \"Transaction_Ratio\": transaction_ratio,\n",
    "                 \"Transfer-In Ratio\": transfer_in_ratio,\n",
    "                 \"Transfer-Out Ratio\": transfer_out_ratio,\n",
    "                 \"Number of Neighbours\": neighbours,\n",
    "                 \"Inverse Timestamp Frequency\": inv_timestamp_freq\n",
    "                 }\n",
    "    data.append(node_dict)"
   ],
   "id": "2a1ef24b23f7ca5",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 4/4874 [00:00<05:46, 14.06it/s]C:\\Users\\zhou\\AppData\\Local\\Temp\\ipykernel_61020\\1673113850.py:18: RuntimeWarning: divide by zero encountered in double_scalars\n",
      "  inv_timestamp_freq = 1/avg_timestamp_diff\n",
      "100%|██████████| 4874/4874 [03:58<00:00, 20.45it/s]\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:17:47.488859Z",
     "start_time": "2025-04-02T13:16:51.499265Z"
    }
   },
   "cell_type": "code",
   "source": [
    "timestamp_nodes = []\n",
    "for i in tqdm(range(len(data))):\n",
    "    fr = list(filtered_df[filtered_df['From']==data[i]['Node']]['TimeStamp'])\n",
    "    to = list(filtered_df[filtered_df['To']==data[i]['Node']]['TimeStamp'])\n",
    "    fr = fr+to\n",
    "    timestamp_nodes.append(fr)\n",
    "    "
   ],
   "id": "e83e287acd3e6b44",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4874/4874 [00:55<00:00, 87.09it/s] \n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:17:52.815055Z",
     "start_time": "2025-04-02T13:17:52.774498Z"
    }
   },
   "cell_type": "code",
   "source": [
    " nodes = [\n",
    "    [node['Total Degree'], node['Out-Degree'], node['In-Degree'],\n",
    "     node['Out-Degree Ratio'], node['In-Degree Ratio'], node['Sum of Transactions'],\n",
    "     node['Transfer-Out Transaction'], node['Transfer-In Transaction'],\n",
    "     node['Transaction Difference'], node['Transaction_Ratio'],\n",
    "     node['Transfer-In Ratio'], node['Transfer-Out Ratio'],\n",
    "     node['Number of Neighbours'], node['Inverse Timestamp Frequency']]\n",
    "    for node in data\n",
    "]\n",
    "\n",
    "edges = [\n",
    "    [ node['Out-Degree'], node['In-Degree'],\n",
    "     node['Out-Degree Ratio'], node['In-Degree Ratio'],\n",
    "     node['Transfer-Out Transaction'], node['Transfer-In Transaction'],\n",
    "     node['Transfer-In Ratio'], node['Transfer-Out Ratio']]\n",
    "    for node in data\n",
    "]"
   ],
   "id": "f92f6ff28dfb3014",
   "outputs": [],
   "execution_count": 66
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:17:58.229217Z",
     "start_time": "2025-04-02T13:17:56.645536Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import gc\n",
    "gc.collect()"
   ],
   "id": "5c3876d1d257155a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6263"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:04.735055Z",
     "start_time": "2025-04-02T13:18:04.710596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class TGATLayer(nn.Module):\n",
    "    def __init__(self, input_dim, edge_dim, hidden_dim, num_heads):\n",
    "        super(TGATLayer, self).__init__()\n",
    "        self.num_heads = num_heads\n",
    "        # Linear projections for 'query', 'key', and 'value'\n",
    "        self.Wq = nn.Linear(input_dim + edge_dim, hidden_dim * num_heads)\n",
    "        self.Wk = nn.Linear(input_dim + edge_dim, hidden_dim * num_heads)\n",
    "        self.Wv = nn.Linear(input_dim + edge_dim, hidden_dim * num_heads)\n",
    "\n",
    "        # Output linear projection\n",
    "        self.Wout = nn.Linear(hidden_dim, hidden_dim * num_heads)\n",
    "        if (self.Wout).in_features == 1:\n",
    "          self.Wout = nn.Linear(hidden_dim * num_heads, hidden_dim)\n",
    "    def forward(self, node_features, edge_features, timestamps_list, num_nodes):\n",
    "        # Concatenate node features and edge features\n",
    "        Z = torch.cat([node_features, edge_features], dim=1)\n",
    "        # Linear projections\n",
    "        Q = self.Wq(Z).view(-1, num_nodes, hidden_dim)\n",
    "        K = self.Wk(Z).view(-1, num_nodes, hidden_dim)\n",
    "        V = self.Wv(Z).view(-1, num_nodes, hidden_dim)\n",
    "\n",
    "        # Concatenate timestamps along a new dimension\n",
    "        timestamps_tensor = torch.stack(timestamps_list, dim=1)\n",
    "\n",
    "        # Attention mechanism\n",
    "        attention_scores = torch.matmul(Q, K.transpose(1, 2)) / torch.sqrt(torch.tensor(hidden_dim, dtype=torch.float32))\n",
    "        attention_weights = F.softmax(attention_scores, dim=-1)\n",
    "        attention_weights = torch.nan_to_num(attention_weights, nan=0)\n",
    "        V = torch.nan_to_num(V, nan=0)\n",
    "        attended_values = torch.matmul(attention_weights, V)\n",
    "        attended_values = attended_values.mean(dim=0, keepdim=True)\n",
    "\n",
    "        # Output linear projection\n",
    "        output = self.Wout(attended_values.view(-1, hidden_dim))\n",
    "        return output"
   ],
   "id": "cf43317b56dddb94",
   "outputs": [],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:07.505660Z",
     "start_time": "2025-04-02T13:18:07.486710Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MultiHeadAttention(nn.Module):\n",
    "    def __init__(self, input_dim, hidden_dim, num_heads, node_features, edge_dim):\n",
    "        super(MultiHeadAttention, self).__init__()\n",
    "        self.num_heads = num_heads\n",
    "        self.head_dim = input_dim // num_heads\n",
    "        self.attention_layers = nn.ModuleList([TGATLayer(input_dim, edge_dim, hidden_dim, num_heads) for _ in range(num_heads)])\n",
    "\n",
    "    def forward(self, node_features, edge_features, timestamps_list, num_nodes):\n",
    "        head_outputs = [layer(node_features, edge_features, timestamps_list, num_nodes) for layer in self.attention_layers]\n",
    "        concatenated_representations = torch.cat(head_outputs, dim=-1)\n",
    "        return concatenated_representations\n"
   ],
   "id": "7c26d02bbb4098ff",
   "outputs": [],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:10.678577Z",
     "start_time": "2025-04-02T13:18:10.659058Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class PDTGAWithGraphAttention(nn.Module):\n",
    "    def __init__(self, input_dim, edge_feature_dim, time_feature_dim, hidden_dim, num_layers, num_heads):\n",
    "        super(PDTGAWithGraphAttention, self).__init__()\n",
    "        self.graph_attention = MultiHeadAttention(input_dim, hidden_dim, num_heads, node_features, edge_features_dim)\n",
    "        self.tgat_layers = nn.ModuleList([TGATLayer(input_dim, edge_feature_dim, time_feature_dim, hidden_dim) for _ in range(num_layers)])\n",
    "        self.FFN = nn.Linear(hidden_dim * (input_dim + time_feature_dim + num_layers) + input_dim + time_dim, hidden_dim)\n",
    "\n",
    "    def forward(self, node_features, edge_features, timestamps, num_nodes):\n",
    "        # Calculate Graph Attention\n",
    "        graph_attention_output = self.graph_attention(node_features, edge_features, timestamps, num_nodes)\n",
    "        hidden_representations = [node_features]\n",
    "        for i, tgat_layer in enumerate(self.tgat_layers):\n",
    "            hidden_representations.append(tgat_layer(hidden_representations[i], edge_features, timestamps, num_nodes))\n",
    "        # Concatenate TGAT output with Graph Attention output\n",
    "        concatenated_representations = torch.cat([graph_attention_output] + hidden_representations, dim=-1)\n",
    "        concatenated_representations = torch.nan_to_num(concatenated_representations, nan=0)\n",
    "        output = self.FFN(concatenated_representations)\n",
    "        return output"
   ],
   "id": "4368bcd64d51286e",
   "outputs": [],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:13.628072Z",
     "start_time": "2025-04-02T13:18:13.617071Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Example usage:\n",
    "num_nodes = len(data)\n",
    "node_features_dim = 14\n",
    "edge_features_dim = 8\n",
    "time_dim = 1\n",
    "hidden_dim = node_features_dim + edge_features_dim\n",
    "num_heads = 4\n",
    "output_dim = 1\n",
    "num_layers = 1"
   ],
   "id": "759a3b5d61f40e39",
   "outputs": [],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:22.050854Z",
     "start_time": "2025-04-02T13:18:17.477493Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# You can then pass your data through the model\n",
    "node_features = torch.tensor(nodes, dtype=torch.float32)\n",
    "edge_features = torch.tensor(edges, dtype=torch.float32)\n",
    "timestamps = [torch.tensor(ts, dtype=torch.float32) for ts in timestamp_nodes]\n",
    "max_length = max(len(ts) for ts in timestamps)\n",
    "padded_timestamps = [F.pad(ts, (0, max_length - len(ts))) for ts in timestamps]\n",
    "\n",
    "gat_model = PDTGAWithGraphAttention(node_features_dim, edge_features_dim, time_dim, hidden_dim, num_layers, num_heads)\n",
    "output = gat_model(node_features, edge_features, padded_timestamps, num_nodes)"
   ],
   "id": "aaa41d3f4314d55e",
   "outputs": [],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:24.685738Z",
     "start_time": "2025-04-02T13:18:24.667581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "output"
   ],
   "id": "770cb6dc57bc973f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.2517e+37,  1.0673e+37,  6.3575e+36,  ...,  2.0183e+36,\n",
       "         -2.1924e+36, -2.0844e+37],\n",
       "        [ 2.4628e+37,  8.6621e+36,  7.1054e+36,  ..., -1.3701e+37,\n",
       "         -1.1453e+37, -2.4673e+37],\n",
       "        [ 1.2517e+37,  1.0673e+37,  6.3575e+36,  ...,  2.0183e+36,\n",
       "         -2.1924e+36, -2.0844e+37],\n",
       "        ...,\n",
       "        [ 2.4628e+37,  8.6621e+36,  7.1054e+36,  ..., -1.3701e+37,\n",
       "         -1.1453e+37, -2.4673e+37],\n",
       "        [ 5.0433e-02, -1.7640e-02,  1.3928e-01,  ..., -7.3166e-02,\n",
       "          1.8986e-02, -4.4250e-02],\n",
       "        [ 2.4670e-01,  1.1403e-01, -2.9107e-01,  ..., -1.0531e-01,\n",
       "         -3.7869e-01, -5.0713e-01]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:18:26.916138Z",
     "start_time": "2025-04-02T13:18:26.889006Z"
    }
   },
   "cell_type": "code",
   "source": [
    "predictions = torch.sigmoid(output)\n",
    "predictions"
   ],
   "id": "74ae1b0569500fae",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.0000, 1.0000, 1.0000,  ..., 1.0000, 0.0000, 0.0000],\n",
       "        [1.0000, 1.0000, 1.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
       "        [1.0000, 1.0000, 1.0000,  ..., 1.0000, 0.0000, 0.0000],\n",
       "        ...,\n",
       "        [1.0000, 1.0000, 1.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
       "        [0.5126, 0.4956, 0.5348,  ..., 0.4817, 0.5047, 0.4889],\n",
       "        [0.5614, 0.5285, 0.4277,  ..., 0.4737, 0.4064, 0.3759]],\n",
       "       grad_fn=<SigmoidBackward0>)"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:22:35.911879Z",
     "start_time": "2025-04-02T13:18:28.574907Z"
    }
   },
   "cell_type": "code",
   "source": [
    "nodes_list = list(graph.nodes())\n",
    "result_data = []\n",
    "\n",
    "for node in tqdm(nodes_list):\n",
    "    is_error_value = int(any(first_order_df[first_order_df['From'] == node]['isError'] == 1) or any(first_order_df[first_order_df['To'] == node]['isError'] == 1))\n",
    "    result_data.append((node, is_error_value))\n",
    "\n",
    "result_df = pd.DataFrame(result_data, columns=['Node', 'isError'])\n",
    "result_df"
   ],
   "id": "2f0ad6a972df421d",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4874/4874 [04:07<00:00, 19.71it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "                                            Node  isError\n",
       "0     0x002f0c8119c16d310342d869ca8bf6ace34d9c39        0\n",
       "1     0xe892875b87b94c44edf0e91ee9f49d0525fadd83        0\n",
       "2     0xcbb240ad8beb648773d78d966caaee46d45add3a        0\n",
       "3     0x0059b14e35dab1b4eee1e2926c7a5660da66f747        1\n",
       "4     0xc90873d0c4b07db2327676d3b3f2add807d5fc0e        0\n",
       "...                                          ...      ...\n",
       "4869  0xa4940f812907115c1d63c5ec998dd195732cb285        0\n",
       "4870  0xfe5abe24fe9de5fa1a3e6d433bddf1278dfa652f        0\n",
       "4871  0x117f674866a87ee44db7bccdb298298aa58229ac        0\n",
       "4872  0xef0e4d16722aeedce4d4f2ca7a16697f5613bfd3        0\n",
       "4873  0x8ac6a668505d4c5f3002d22fa03c657a32bf98e1        0\n",
       "\n",
       "[4874 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Node</th>\n",
       "      <th>isError</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0x002f0c8119c16d310342d869ca8bf6ace34d9c39</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0xe892875b87b94c44edf0e91ee9f49d0525fadd83</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0xcbb240ad8beb648773d78d966caaee46d45add3a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0x0059b14e35dab1b4eee1e2926c7a5660da66f747</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0xc90873d0c4b07db2327676d3b3f2add807d5fc0e</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4869</th>\n",
       "      <td>0xa4940f812907115c1d63c5ec998dd195732cb285</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4870</th>\n",
       "      <td>0xfe5abe24fe9de5fa1a3e6d433bddf1278dfa652f</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4871</th>\n",
       "      <td>0x117f674866a87ee44db7bccdb298298aa58229ac</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4872</th>\n",
       "      <td>0xef0e4d16722aeedce4d4f2ca7a16697f5613bfd3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4873</th>\n",
       "      <td>0x8ac6a668505d4c5f3002d22fa03c657a32bf98e1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4874 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:22:48.264077Z",
     "start_time": "2025-04-02T13:22:48.242411Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Import necessary libraries\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "X = output\n",
    "\n",
    "# Convert 'IsError' column to a tensor\n",
    "y = torch.tensor(result_df['isError'].values, dtype=torch.float32)\n",
    "\n",
    "# Split the data into training and testing sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "# Define your MLP model\n",
    "class MLPClassifier(nn.Module):\n",
    "    def __init__(self, input_dim, hidden_dim, output_dim):\n",
    "        super(MLPClassifier, self).__init__()\n",
    "        self.fc1 = nn.Linear(input_dim, hidden_dim)\n",
    "        self.relu = nn.ReLU()\n",
    "        self.fc2 = nn.Linear(hidden_dim, output_dim)\n",
    "        self.sigmoid = nn.Sigmoid()\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.fc1(x)\n",
    "        x = self.relu(x)\n",
    "        x = self.fc2(x)\n",
    "        x = self.sigmoid(x)\n",
    "        return x\n",
    "# Instantiate the model\n",
    "embedding_dim = X_train.shape[1]\n",
    "hidden_dim = 64\n",
    "output_dim = 1\n",
    "mlp_model = MLPClassifier(embedding_dim, hidden_dim, output_dim)\n",
    "\n",
    "# Forward pass on the test set\n",
    "with torch.no_grad():\n",
    "    test_outputs = mlp_model(X_test)\n",
    "    predictions = (test_outputs.squeeze() > 0.5).float()\n",
    "    accuracy = accuracy_score(y_test.numpy(), predictions.numpy())\n",
    "    print(f'Accuracy on the test set: {accuracy * 100:.2f}%')\n"
   ],
   "id": "f737eb548ad3d94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy on the test set: 84.31%\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:22:51.687815Z",
     "start_time": "2025-04-02T13:22:51.659453Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    "precision = precision_score(y_test.numpy(), predictions.numpy())\n",
    "recall = recall_score(y_test.numpy(), predictions.numpy())\n",
    "f1 = f1_score(y_test.numpy(), predictions.numpy())\n",
    "\n",
    "print(f'Precision: {precision * 100:.2f}%')\n",
    "print(f'Recall: {recall * 100:.2f}%')\n",
    "print(f'F1 Score: {f1 * 100:.2f}%')\n"
   ],
   "id": "218563a349d58138",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Precision: 11.54%\n",
      "Recall: 5.31%\n",
      "F1 Score: 7.27%\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T13:22:56.529331Z",
     "start_time": "2025-04-02T13:22:56.444359Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import os\n",
    "\n",
    "# 确保model目录存在\n",
    "os.makedirs('model', exist_ok=True)\n",
    "\n",
    "# 保存PDTGA模型\n",
    "torch.save(gat_model.state_dict(), 'model/pdtga_model.pth')\n",
    "print(\"PDTGA模型已保存到 model/pdtga_model.pth\")\n",
    "\n",
    "# 保存MLP分类器模型\n",
    "torch.save(mlp_model.state_dict(), 'model/mlp_classifier.pth')\n",
    "print(\"MLP分类器模型已保存到 model/mlp_classifier.pth\")\n",
    "\n",
    "# 保存模型参数信息，便于后续加载模型\n",
    "model_info = {\n",
    "    'node_features_dim': node_features_dim,\n",
    "    'edge_features_dim': edge_features_dim,\n",
    "    'time_dim': time_dim,\n",
    "    'hidden_dim': hidden_dim,\n",
    "    'num_heads': num_heads,\n",
    "    'num_layers': num_layers,\n",
    "    'embedding_dim': embedding_dim,\n",
    "    'mlp_hidden_dim': hidden_dim,\n",
    "    'output_dim': output_dim\n",
    "}\n",
    "\n",
    "import json\n",
    "with open('model/model_info.json', 'w') as f:\n",
    "    json.dump(model_info, f)\n",
    "print(\"模型参数信息已保存到 model/model_info.json\")"
   ],
   "id": "ee18e14978f8e9f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PDTGA模型已保存到 model/pdtga_model.pth\n",
      "MLP分类器模型已保存到 model/mlp_classifier.pth\n",
      "模型参数信息已保存到 model/model_info.json\n"
     ]
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [],
   "id": "abf327ffd4395452"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
